//
//  SearchResultViewController.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/6.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "SearchResultViewController.h"
#import "ActionTableViewCell.h"
#import "GoodModel.h"
#import "GoodInfoViewController.h"

@interface SearchResultViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) UIView *headerView;
@property (nonatomic,strong) UIView *footerView;

@property (nonatomic,strong) NSArray *dataArray;
@property (nonatomic,strong) NSDictionary *dataDictionary;

@end

//网络
@implementation SearchResultViewController (NetWorking)

- (void)getData{
    [self showprogressHUD];
    NSDictionary *parameters = @{@"goodsName":self.goodsName};
    [[AFAppDotNetAPIClient sharedClient] POST:@"goodVOController/queryGoods.action" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"responseState"] integerValue] == 0) {
            self.dataArray = [NSArray yy_modelArrayWithClass:[GoodModel class] json:responseObject[@"obj"]];
            [self.tableView reloadData];
        }else{
            ShowMessage(responseObject[@"msg"]);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}

@end

//UI
@implementation SearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"";
    [self.view addSubview:self.tableView];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"] style:0 target:self action:@selector(searchEvent)];
    self.navigationItem.titleView = ({
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 7, SCREEN_WIDTH, 30)];
        btn.layer.borderWidth = .5;
        btn.layer.borderColor = [UIColor whiteColor].CGColor;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 3;
        [btn setTitle:@"搜索商品" forState:UIControlStateNormal];
        btn.titleLabel.font = DEF_FontSize_12;
        [btn addTarget:self action:@selector(searchEvent) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self getData];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

#pragma mark - Event

- (void)searchEvent{
    [self.navigationController popViewControllerAnimated:NO];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ActionTableViewCell class])];
    cell.model = self.dataArray[indexPath.section];
    cell.vc = self;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GoodInfoViewController *vc = [GoodInfoViewController new];
    vc.gId = [self.dataArray[indexPath.section] gId];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - set & get

- (UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionFooterHeight = 5;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footerView;
        _tableView.tableHeaderView = self.headerView;
        _tableView.rowHeight = 70;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [_tableView registerClass:[ActionTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ActionTableViewCell class])];
    }
    return _tableView;
}

- (UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _headerView;
}

- (UIView *)footerView{
    if (!_footerView) {
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.01)];
    }
    return _footerView;
}

- (NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = @[];
    }
    return _dataArray;
}

- (NSDictionary *)dataDictionary{
    if (!_dataDictionary) {
        _dataDictionary = @{};
    }
    return _dataDictionary;
}

@end
